<template>
  <div class="app-container">
    <el-row class="library-row">
      <el-col :span="4">
        <el-input placeholder="请输入内容" v-model="search" class="input-with-select">
          <el-button slot="append" icon="el-icon-search" @click="searchList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="3" :offset="17" class="add">
        <el-button @click="addKeywords" type="primary" icon="el-icon-plus">添加关键字</el-button>
      </el-col>
    </el-row>
    <el-table
      :data="tableData"
      stripe
      @sort-change="sort"
      style="width: 100%"
      :default-sort="{prop: 'id', order: 'descending'}"
    >
      <el-table-column prop="id" label="#" width="100" sortable="custom"></el-table-column>
      <el-table-column prop="name" label="关键字"></el-table-column>
      <el-table-column fixed="right" label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="editor(scope.row)">编辑</el-button>
          <el-button @click="del(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row class="total">
      <el-pagination
        @current-change="currentPage"
        background
        :page-size="20"
        layout="prev, pager, next"
        :total="total"
      ></el-pagination>
    </el-row>

    <!-- 弹窗 -->
    <el-dialog
      :title="title"
      class="school"
      :close-on-click-modal="false"
      width="20%"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="formData" ref="formSave" :rules="rules">
        <el-form-item label="关键字" prop="name">
          <el-input v-model="formData.name" placeholder="请填写" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="formData.type" placeholder="请选择">
            <el-option
              v-for="item in formDataType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save('formSave')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  keywordList,
  addKeyword,
  delKeyword,
  editorKeyword,
  editorKeywordList,
  sortKeyword,
} from "@/api/set";

export default {
  data() {
    return {
      title: "添加关键字",
      search: "",
      total: 1,
      isEdit: false,
      form: {
        page: 1,
        page_size: 20,
        name: "",
        sort: "",
        order: "",
      },
      formData: {
        name: "",
        type:null
      },
      rules: {
        name: [{ required: true, message: "请填写关键字", trigger: "blur" }],
      },
      id: null,
      dialogFormVisible: false, //新增弹窗
      list: null,
      listLoading: true,
      value: "",
      tableData: [],
      formDataType:[{
          value: 1,
          label: '图书'
        }, {
          value: 2,
          label: '音频'
        }, 
      ],
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      keywordList().then((response) => {
        console.log(response);
        this.total = response.total;
        this.tableData = response.data;
      });
    },
    // 排序及搜索请求
    async changedata(order) {
      let params = {
        page: 1,
        page_size: 20,
        name: this.search,
        sort: "id",
        order: order,
      };
      let list = await keywordList(params);
      this.tableData = list.data;
    },
    // 删除请求
    async delete() {
      let id = this.id;
      let del = await delKeyword(id);
      if (del.code == 1) {
        this.$message({
          type: "success",
          message: del.message,
        });
      } else {
        this.$message({
          type: "info",
          message: del.message,
        });
      }
      this.fetchData();
    },
    // 保存新增请求
    async saveData() {
      let data = this.formData;
      let res = await addKeyword(data);
      console.log(res)
      if (res.code == 1) {
        this.$message({
          type: "success",
          message: res.message,
        });
        this.fetchData();
        this.dialogFormVisible = false;
      } else {
        this.$message({
          type: "info",
          message: res.message,
        });
      }
    },
    // 保存编辑请求
    async saveDataEditor() {
      this.formData.$id = this.id;
      let data = this.formData;
      console.log(data)
      let res = await editorKeyword(data,this.id);
      console.log(res)
      if (res.code == 1) {
        this.$message({
          type: "success",
          message: res.message,
        });
        this.fetchData();
        this.dialogFormVisible = false;
      } else {
        this.$message({
          type: "info",
          message: res.message,
        });
      }
    },
    // 获取编辑请求
    async editorData() {
      let $id = this.id;
      let res = await editorKeywordList($id);
      if (res.code == 1) {
        this.$message({
          type: "success",
          message: res.message,
        });
        this.formData = res.data;
      } else {
        this.$message({
          type: "info",
          message: res.message,
        });
      }
    },
    // 搜索
    searchList() {
      (this.form.page = 1), (this.form.unit_name = this.search);
      this.changedata();
    },
    // 添加关键字
    addKeywords() {
      //  this.$refs.formSave.clearValidate()
      setTimeout(() => {
        console.log(this.$refs.formSave);
        this.$nextTick(function () {
          this.$refs.formSave.clearValidate();
        });
      }, 50);
      this.dialogFormVisible = true;
      this.title = "添加关键字";
      this.formData = {};
    },
    // 编辑
    editor(row) {
      console.log(row);
      this.id = row.id;
      this.dialogFormVisible = true;
      this.title = "编辑年级";
      this.editorData();
    },
    // 删除
    del(row) {
      this.id = row.id;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delete();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 排序
    sort(val) {
      let order = val.order;
      if (order == "descending") {
        this.form.order = "desc";
      } else if (order == "ascending") {
        this.form.order = "asc";
      } else {
        this.form.order = null;
      }
      this.changedata(this.form.order);
    },
    // 页签
    currentPage(val) {
      this.form.page = val;
      this.changedata();
    },
    // 保存数据
    save(formSave) {
      this.$refs[formSave].validate((valid) => {
        if (valid) {
          if (this.title == "添加关键字") {
            this.saveData();
            console.log(1);
          } else {
            this.saveDataEditor();
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style>
.library-row {
  padding: 15px 0;
}
.add {
  display: flex;
  justify-content: flex-end;
}
.el-dialog__body {
  border: 1px solid #ddd;
}
.total {
  display: flex;
  justify-content: center;
  padding: 40px 0;
}
.el-form-item__label {
  float: none;
}
.school .el-select {
  display: inline-block;
  width: 100%;
  position: relative;
}
</style>